package net.yebaihe.wc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.mobclick.android.MobclickAgent;

import net.yebaihe.wc.DataMan.MSGTYPE;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class Index extends StateMachine{

	class IndexSimpleAdapter extends SimpleAdapter{

		private ArrayList<HashMap<String, Object>> mData;
		private int mResource;
		private LayoutInflater mInflater;

		public IndexSimpleAdapter(Index index,
				ArrayList<HashMap<String, Object>> meumList, int indexItem,
				String[] strings, int[] is) {
			super(index,meumList,indexItem,strings,is);
			mData = meumList;
			mResource = indexItem;
			mInflater = (LayoutInflater) index.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		
		public View getView(int position, View convertView, ViewGroup parent) {
	        return createViewFromResource(position, convertView, parent, mResource);
	    }

		private View createViewFromResource(int position, View convertView,
	            ViewGroup parent, int resource) {
	        View v;
	        if (convertView == null) {
	            v = mInflater.inflate(resource, parent, false);
	        } else {
	            v = convertView;
	        }

	        bindView(position, v);

	        return v;
	    }

		private void bindView(int position, View view) {
	        final Map<String, Object> map = mData.get(position);
	        if (map == null) {
	            return;
	        }
	        Button b=(Button) view.findViewById(R.id.btnIndex);
	        b.setBackgroundResource((Integer) map.get("ItemImage"));
	        b.setTag(map.get("ItemImage"));
	        b.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					indexClicked((Integer) v.getTag());
				}
	        });
	        TextView v=(TextView)view.findViewById(R.id.ItemText);
	        v.setText(map.get("ItemText").toString());
		}

	}

	public static final String PREFS_NAME = "weican";

	protected void indexClicked(int tag) {
		switch (tag){
		case R.drawable.b_more:{
			changeToActivity(IndexSub.class);
		}
			break;
		case R.drawable.b_map:{
			if ((getWCAppContext().cellcity.length()>0) && (!getWCAppContext().cellcity.equals(getWCAppContext().city))){
				new AlertDialog.Builder(this).setTitle("城市变更")
					.setMessage("可以由("+getWCAppContext().city+")自动切换到您所在的城市("+getWCAppContext().cellcity+")，要这样做吗？")
					.setPositiveButton("是的", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							getWCAppContext().otherRestaurantSql="";
							getWCAppContext().city=getWCAppContext().cellcity;
							Pref.get(Index.this).save("city", Index.this.getWCAppContext().city);
							changeToActivity(NearBy.class);
						}
					})
					.setNegativeButton("不用", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							getWCAppContext().otherRestaurantSql="";
							changeToActivity(NearBy.class);
						}
					}).create().show();
			}
			else{
				getWCAppContext().otherRestaurantSql="";
				changeToActivity(NearBy.class);
			}
		}
		break;
		case R.drawable.b_friend:{
			if (checkLogin(true)){
				changeToActivity(Friends.class);
			}
		}
		break;
		case R.drawable.b_dinner:{
			if (checkLogin(true)){
				changeToActivity(Sessions.class);
			}
		}
			break;
		case R.drawable.b_col:
			if (checkLogin(true)){
				changeToActivity(Favorites.class);
			}
			break;
		case R.drawable.b_sear:
			changeToActivity(Search.class);
			break;
		}
	}
	
	@Override
	protected String getTopbarTitle() {
		return "微餐";
	}

	@Override
	protected void prepareClient(View client) {
		MobclickAgent.onError(this);
		GridView g1 = (GridView) client.findViewById(R.id.GridView1);
		
		String idxBtnTitles[]={
			"附近",
			"搜索",
			"收藏",
		};
		int idxBtnImages[]={
			R.drawable.b_map,
			R.drawable.b_sear,
			R.drawable.b_col,
		};
		
		prepareLevel(idxBtnTitles,idxBtnImages,g1);

		GridView g2 = (GridView) client.findViewById(R.id.GridView2); 
		
		String idxBtnTitles2[]={
			"我的好友",
			"我的聚餐",
			"更多",
		};
		int idxBtnImages2[]={
			R.drawable.b_friend,
			R.drawable.b_dinner,
			R.drawable.b_more,
		};
		prepareLevel(idxBtnTitles2,idxBtnImages2,g2);
	}

	private void prepareLevel(String[] idxBtnTitles, int[] idxBtnImages,
			GridView g1) {
		ArrayList<HashMap<String, Object>> meumList = new ArrayList<HashMap<String, Object>>(); 

		for(int i = 1;i < idxBtnTitles.length+1;i++) { 
			HashMap<String, Object> map = new HashMap<String, Object>(); 
			map.put("ItemImage", idxBtnImages[i-1]); 
			map.put("ItemText", idxBtnTitles[i-1]); 
			meumList.add(map); 
		} 

		
		IndexSimpleAdapter saMenuItem = new IndexSimpleAdapter(this, 
				meumList, //数据源 
				R.layout.index_item, //xml实现 
				new String[]{"ItemText"}, //对应map的Key 
				new int[]{R.id.ItemText});  //对应R的Id 

		//添加Item到网格中 
		g1.setAdapter(saMenuItem); 
		
	}

	@Override
	public void HandleMessage(MSGTYPE msgtype, boolean ret,
			HashMap<String, String> params) {
		super.HandleMessage(msgtype, ret, params);
	}

	@Override
	protected int getLayoutRes() {
		return R.layout.index;
	}

	@Override
	public void onBackPressed(){
		new AlertDialog.Builder(this).setIcon(R.drawable.icon).setTitle("确认退出")
			 .setMessage("你确定要退出"+getString(R.string.app_name)+"吗？")
			 .setCancelable(false)
			 .setPositiveButton("确定", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					getWCAppContext().inited=false;
					finish();
				}
			}).setNegativeButton("取消", null).create().show();
	}
}
